package cn.sh.ideal.web.login.menu.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.sh.ideal.web.login.menu.dao.MenuMapper;
import cn.sh.ideal.web.login.menu.service.MenuService;
import cn.sh.ideal.web.login.menu.vo.MenuVo;

import com.alibaba.fastjson.JSONArray;

/**
 * 数据接口实现
 *
 */
@Service
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper mapper;

	/**
	 * 获取菜单数据
	 */
	@Override
	public Object getMenuData(List<String> menuIds) {
		// 获取数据库菜单数据
		List<MenuVo> menuVoList = mapper.selectMenuDataByMenuIds(menuIds);
		// 创建父节点菜单
		List<MenuVo> parentEntityList = new ArrayList<MenuVo>();
		// 创建子节点菜单
		List<MenuVo> childrenEntiyList = new ArrayList<MenuVo>();
		//创建三级菜单
		List<MenuVo> treeMenuEntiyList = new ArrayList<MenuVo>();
		
		// 创建最终结果菜单
		List<Object> resultList = new ArrayList<Object>();
		// 获取节点数据
		for (MenuVo menuVo : menuVoList) {
			if (menuVo.getId() == menuVo.getParentId()) {
				// 获取父节点菜单
				parentEntityList.add(menuVo);
			} else {
					// 获取子节点菜单
					childrenEntiyList.add(menuVo);
			}
			if(menuVo.getParentId() == 0){
				//获取三级菜单
				treeMenuEntiyList.add(menuVo);
			}
		}
		// 创建结果菜单
		for (MenuVo parent : parentEntityList) {
			// 临时子菜单
			List<Object> childLis = new ArrayList<Object>();
			// 临时结果菜单
			LinkedHashMap<String, Object> temporaryMenu = new LinkedHashMap<String, Object>();
			for (MenuVo children : childrenEntiyList) {
				LinkedHashMap<String, Object> temporaryChildMenu = new LinkedHashMap<String, Object>();
				List<Object> treeMenuChild = new ArrayList<Object>();
				if (parent.getId() == children.getParentId()) {
					for(MenuVo tree:treeMenuEntiyList){
						LinkedHashMap<String, Object> treeMenu = new LinkedHashMap<String, Object>();
						if(children.getId() == tree.getTreeMenuId()){
							treeMenu.put("name", tree.getName());
							treeMenu.put("url", tree.getUrl());
							treeMenu.put("imgClass", tree.getImgClass());
							treeMenu.put("children", tree.getChild());
							treeMenuChild.add(treeMenu);
						}
						
					}
					if(parent.getId() != children.getTreeMenuId()){
						temporaryChildMenu.put("name", children.getName());
						temporaryChildMenu.put("url", children.getUrl());
						temporaryChildMenu.put("imgClass", children.getImgClass());
					}

					if(treeMenuChild !=null  && treeMenuChild.size() > 0){
						temporaryChildMenu.put("children", treeMenuChild);
					}else{
						temporaryChildMenu.put("children", null);
					}
					childLis.add(temporaryChildMenu);
				}
			}
			temporaryMenu.put("name", parent.getName());
			temporaryMenu.put("url", parent.getUrl());
			temporaryMenu.put("imgClass", parent.getImgClass());
			if (childLis.size() > 0 && childLis != null) {
				temporaryMenu.put("children", childLis);
			} else {
				temporaryMenu.put("children", null);
			}
			resultList.add(temporaryMenu);
		}
		// 转成json数组
		Object jsonArray = JSONArray.toJSON(resultList);
		return jsonArray;
	}

}
